import Vue from 'vue'
import Vuex from 'vuex'
import qs from 'qs'

Vue.use(Vuex)

export default new Vuex.Store({
    //保存共享数据
    state: {
        pageSize: 5,
        total: null,
        categoryInfo: {
            id: null,
            name: null
        },
        categories: null,
        backEndUrl: {
            queryCategoriesByLimit: '/category/queryCategoriesByLimit',
            queryAllCategories: '/category/queryCategoriesAll',
            queryCategoryById: '/category/queryCategoryById',
            updateUrl: '/category/updateCategory',
            addUrl: '/category/addCategory',
            deleteUrl: '/category/deleteCategory',
            checkExistUrl: '/category/checkExist'
        },
        routeUrl: {
            categoryList: '/category/list',
        },
        rules: {
            name: [
                {required: true, message: '请输入分类名称', trigger: 'blur'},
                {pattern: /^[a-zA-Z\u4e00-\u9fa5]+$/, message: "请勿输入特殊字符", trigger: "blur"},
                {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
            ],
        }
    },
    mutations: {
        setCategories(state, categories) {
            state.categories = categories;
        }, setTotal(state, total) {
            state.total = total;
        }
    },
    actions: {
        queryCategoriesByLimit(store, page) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.queryCategoriesByLimit, qs.stringify({
                    pageNo: page.pageNo,
                    pageSize: page.pageSize
                })).then(resp => {
                    store.commit('setCategories', resp.data.list);
                    store.commit('setTotal', resp.data.total);
                    resolve(true);
                }).catch(() => resolve(false));
            });
        },
        queryCategoriesAll(store) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.queryAllCategories).then(resp => {
                    store.commit('setCategories', resp.data);
                    resolve(resp.data);
                }).catch(() => resolve(null));
            });
        },
        deleteCategoryById(store, categoryId) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.deleteUrl, qs.stringify({id: categoryId})).then(resp => {
                    resolve(resp);
                }).catch(() => {
                    resolve(false);
                });
            });
        },
        checkCategoryExist(store, category) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.checkExistUrl, qs.stringify({name: category.name})).then(resp => {
                    resolve(resp.data); //差点忘了data
                }).catch(() => {
                    resolve(false);
                });
            });
        },
        addCategory(store, categoryInfo) {
            return new Promise(resolve => {
                axios.post(store.state.backEndUrl.addUrl, categoryInfo, {
                    headers: {
                        'content-type': 'application/json' //@requestBody,不能用qs
                    }
                }).then(resp => {
                    resolve(resp.data);
                }).catch(() => resolve(false));
            });
        },
        queryCategoryById(store, id) {
            return new Promise((resolve) => {
                axios.post(store.state.backEndUrl.queryCategoryById, qs.stringify({
                    id: id
                })).then(function (resp) {
                    resolve(resp.data);
                }).catch(() => {
                    resolve(null);
                });
            });
        }, updateCategory(store, category) {
            return new Promise(resolve => {
                axios.post(store.state.backEndUrl.updateUrl, category, {
                    headers: {
                        'content-type': 'application/json' //@requestBody,不能用qs
                    }
                }).then(resp => {
                    resolve(resp.data);
                }).catch(() => resolve(false));
            });
        }
    },
    modules: {},
    getters: {
        getCategoryInfo(state) {
            return state.categoryInfo;
        }, getTotal(state) {
            return state.total;
        }, getPageSize(state) {
            return state.pageSize;
        }, getCategories(state) {
            return state.categories;
        }, getRules(state) {
            return state.rules;
        }
    }
})
